package xyz.myachin.downloader.settings

import android.content.ComponentName
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
import androidx.preference.PreferenceManager
import xyz.myachin.downloader.ui.SettingsActivity
import xyz.myachin.downloader.ui.share.ProxySharingActivity
import xyz.myachin.downloader.ui.share.SharingType

object Settings {
    private const val settings_version = "settings_version"

    private const val metered = "metered_networks"
    private const val roaming = "roaming_networks"
    private const val notification = "when_show_notification"

    private const val autoClean = "do_auto_clean"
    private const val cleanDelay = "clean_delay"
    private const val cleanAc = "clean_must_ac"
    private const val cleanBattery = "clean_must_battery"

    private const val hide_launcher_icon = "hide_launcher_icon"
    private const val key_file_redirection = "key_file_redirection"
    private const val proxy_must_disabled = "proxy_must_disabled"

    private lateinit var mMainPrefs: SharedPreferences
    private lateinit var mAdditionalPrefs: SharedPreferences // not saves in Google Backup
    private val mMainEditor by lazy { mMainPrefs.edit() }
    private val mAdditionalEditor by lazy { mAdditionalPrefs.edit() }

    fun setup(context: Context) {
        mMainPrefs = PreferenceManager.getDefaultSharedPreferences(context)
        mAdditionalPrefs = context.getSharedPreferences("additional_prefs", Context.MODE_PRIVATE)
    }

    fun isAllowedDownloadOverMetered() = mMainPrefs.getBoolean(metered, true)

    fun isAllowDownloadOverRoaming() = mMainPrefs.getBoolean(roaming, false)

    fun howShowDownloadNotification() = mMainPrefs.getString(notification, "1")!!.toInt()

    fun isAutoClean() = mMainPrefs.getBoolean(autoClean, true)

    fun delayClean() = mMainPrefs.getInt(cleanDelay, 20).toLong()

    fun isNeedAcClean() = mMainPrefs.getBoolean(cleanAc, false)

    fun isNeedBattery() = mMainPrefs.getBoolean(cleanBattery, true)

    fun setLauncherIconState(context: Context, state: Int) {
        val cmp = ComponentName.createRelative(context, SettingsActivity::class.java.name)
        context.packageManager.setComponentEnabledSetting(cmp, state, PackageManager.DONT_KILL_APP)
    }

    fun setProxyFeatureState(context: Context, state: Int) {
        val cmp = ComponentName.createRelative(context, ProxySharingActivity::class.java.name)
        context.packageManager.setComponentEnabledSetting(cmp, state, PackageManager.DONT_KILL_APP)
    }

    private fun setAdditionalPrefsBool(key: String, value: Boolean) {
        mAdditionalEditor.putBoolean(key, value)
        mAdditionalEditor.apply()
    }

    private fun setAdditionalPrefsString(key: String, value: String) {
        mAdditionalEditor.putString(key, value)
        mAdditionalEditor.apply()
    }

    var hideLauncherIcon: Boolean
        get() = mAdditionalPrefs.getBoolean(hide_launcher_icon, false)
        set(value) = setAdditionalPrefsBool(hide_launcher_icon, value)

    var isProxyEnabled: Boolean
        get() = mAdditionalPrefs.getBoolean(key_file_redirection, true)
        set(value) = setAdditionalPrefsBool(key_file_redirection, value)

    var isProxyMustDisabled: Boolean
        get() = mAdditionalPrefs.getBoolean(proxy_must_disabled, true)
        set(value) = setAdditionalPrefsBool(proxy_must_disabled, value)

    var versionSettings: String
        get() = mMainPrefs.getString(settings_version, "")!!
        set(value) {
            mMainEditor.putString(settings_version, value)
            mMainEditor.apply()
        }

    fun writeDownloadTaskIdWithType(taskId: Long, sharingType: SharingType) {
        setAdditionalPrefsString(taskId.toString(), sharingType.name)
    }

    fun getDownloadTaskIdType(taskId: Long): SharingType {
        return SharingType.valueOf(
            mAdditionalPrefs.getString(
                taskId.toString(),
                SharingType.MANUAL.name
            )!!
        )
    }

    fun removeDownloadTaskId(vararg taskIds: Long) {
        var apply = false
        taskIds.forEach {
            if (getDownloadTaskIdType(it) == SharingType.DIRECT) {
                mAdditionalEditor.remove(it.toString())
                apply = true
            }
        }
        if (apply) mAdditionalEditor.apply()
    }
}